package com.leetcode.LC;

public class LC13 {
    public int romanToInt(String s) {
        char[] chars = s.toCharArray();//利用toCharArray方法转换
        int[] nums = new int[s.length()];
        int index = 0;
        int result = 0;
        for (char c1 : chars) {
            switch (c1) {
                case 'I':
                    nums[index++] = 1;
                    break;
                case 'V':
                    nums[index++] = 5;
                    break;
                case 'X':
                    nums[index++] = 10;
                    break;
                case 'L':
                    nums[index++] = 50;
                    break;
                case 'C':
                    nums[index++] = 100;
                    break;
                case 'D':
                    nums[index++] = 500;
                    break;
                case 'M':
                    nums[index++] = 1000;
                    break;
            }
        }

        //result=nums[0];
        if(index==1){
            result=nums[0];
        }
        for (int i = 1; i < index; ) {
            if (nums[i-1] < nums[i]) {
                result += nums[i] - nums[i-1];
                i=i+2;
                if (i==index)
                    result+=nums[i-1];
            } else {
                result += nums[i-1];
                i=i+1;
                if (i==index)
                    result+=nums[i-1];
            }
        }

        return result;
    }

    public static void main(String[] args) {
        LC13 lc13 = new LC13();
        int i = lc13.romanToInt("D");//MDCXCV
        System.out.println("结果是");
        System.out.println(i);
    }
}

/*
com.eclipsesource.json.ParseException: Expected ',' or ']' at 1:3
  at line 379, com.eclipsesource.json.JsonParser.error
  at line 372, com.eclipsesource.json.JsonParser.expected
  at line 125, com.eclipsesource.json.JsonParser.readArray
  at line 92, com.eclipsesource.json.JsonParser.readValue
  at line 73, com.eclipsesource.json.JsonParser.parse
  at line 126, com.eclipsesource.json.JsonValue.readFrom
  at line 133, com.eclipsesource.json.JsonArray.readFrom
	at __Deserializer__.toString(Unknown Source)
  at line 80, __Driver__.main
  */